package com.shk.service.imp;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shk.dao.MenuDao;
import com.shk.dao.PermissionDao;
import com.shk.dao.RoleDao;
import com.shk.entity.PageResult;
import com.shk.pojo.Menu;
import com.shk.pojo.Permission;
import com.shk.pojo.Role;
import com.shk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        setRoleAndPermission(role.getId(),permissionIds);
        serRoleAndMenu(role.getId(),menuIds);

    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
       Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        deleteRoleAndMenuAndPermission(id);
        roleDao.delete(id);
    }

    @Override
    public Map findPermissionIdsAndMenusIdsByRoleId(Integer roleId) {

        Role role = roleDao.findById(roleId);

        List<Permission> list = permissionDao.findAll();
       List<Integer> permissionIds = roleDao.findPermissionIdsByRoleId(roleId);
        List<Menu> list1 = menuDao.findAll();

        List<Integer> menuIds = roleDao.findMenusIdsByRoleId(roleId);
        Map map = new HashMap<>();
        map.put("role",role);
        map.put("permissions",list);
        map.put("permissionIds",permissionIds);

        map.put("menus",list1);
        map.put("menuIds",menuIds);
        return map;
    }

    @Override
    public void update(Role role, Integer[] permissionIds, Integer[] menuIds) {

        deleteRoleAndMenuAndPermission(role.getId());

       List<Integer> ids= roleDao.findByIdMenu(role.getId());
        System.out.println(ids);

        roleDao.update(role);

        List<Integer> idss= roleDao.findByIdMenu(role.getId());
        System.out.println(idss);

        setRoleAndPermission(role.getId(),permissionIds);

        serRoleAndMenu(role.getId(),menuIds);

    }

    @Override
    public List<Role> findAll() {
       List<Role> list = roleDao.findAll();
        return list;
    }

    /*建立角色和权限关系*/
    public void setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        if(roleId !=null){
           if(permissionIds != null && permissionIds.length>0){

               for (Integer permissionId:permissionIds){
                   Map map = new HashMap<>();
                   map.put("permission_id",permissionId);
                   map.put("role_id",roleId);
                   roleDao.setRoleAndPermission(map);
               }
           }
        }
    }
    /*建立角色和菜单关系*/
    public void serRoleAndMenu(Integer roleId,Integer[] menuIds){
        if(roleId != null){
            if(menuIds != null && menuIds.length>0){
                for(Integer menuId:menuIds){
                    Map map = new HashMap<>();
                    map.put("role_id",roleId);
                    map.put("menu_id",menuId);
                    roleDao.serRoleAndMenu(map);
                }
            }
        }
    }
    /*删除 三表关系*/
    public void deleteRoleAndMenuAndPermission(Integer id){
        roleDao.deleteRoleAndMenu(id);
        roleDao.deleteRoleAndPermission(id);
    }


}
